package pers.crayon.baseprogram.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 简单定时任务
 *
 * @author wangzg13
 * @since 2022/10/16
 */
public class ScheduledExecutorServiceTask {


    private static SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static void main(String[] args) throws InterruptedException {
        executePeriodicityThreadTest();
    }

    public static void executePeriodicityThreadTest() throws InterruptedException {
        System.out.println(sDateFormat.format(new Date()).toString() + ":");
        executePeriodicityThread(2000, 2000, 5, TimeUnit.MILLISECONDS);
        Thread.sleep(3000);
        System.out.println("=================");
        executePeriodicityThread(2000, 2000, 5, TimeUnit.MILLISECONDS);
    }

    public static void executeDelayThreadTest() throws InterruptedException {
        ScheduledExecutorService scheduledThreadPool = TaskExecutors.getScheduledThreadPool();
        System.out.println(sDateFormat.format(new Date()).toString() + ":任务开始");
        for (int i = 0; i < 12; i++) {
            executeDelayThread(2000, i);
        }
        Thread.sleep(3000);
        System.out.println("延期任务执行完成,当前活跃线程数为:" + ((ThreadPoolExecutor) scheduledThreadPool).getActiveCount());
    }

    /**
     * 延迟任务执行
     */
    public static void executeDelayThread(int delay, int count) {
        ScheduledExecutorService scheduledThreadPool = TaskExecutors.getScheduledThreadPool();
        scheduledThreadPool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.print(sDateFormat.format(new Date()).toString() + ":");
                System.out.println("当前线程:" + Thread.currentThread().getName() + ":" + count);
            }
        }, delay, TimeUnit.MILLISECONDS);
    }

    /**
     * 周期性执行任务
     * 1、怎么解决一个任务启动两次的情况
     * (启动时维护和管理一个map?如果值不为空则提示已经启动任务)
     * <p>
     * 2、怎么指定周期性任务的次数
     * 使用计数器
     *
     * @param delay
     * @param period
     * @param count
     */
    public static void executePeriodicityThread(int delay, int period, int count, TimeUnit unit) {
        ScheduledExecutorService scheduledThreadPool = TaskExecutors.getScheduledThreadPool();
        final int[] tempCount = {0};
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                tempCount[0] += 1;
                if (tempCount[0] <= count) {
                    System.out.println("执行第" + tempCount[0] + "次" + sDateFormat.format(new Date()).toString());
                    System.out.println("当前线程:" + Thread.currentThread().getName());
                }
            }
        }, delay, period, unit);
    }

    public static void convertCornToTimeUnit(String corn) {
        int period = 0;
        TimeUnit unit = TimeUnit.MILLISECONDS;
    }
}
